package com.emep.changzhi.analyse.service.aqi;

import com.emep.changzhi.analyse.entity.aqi.Weather;
import com.emep.changzhi.analyse.repository.aqi.WeatherRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author yangj
 * @version V1.0
 * @Description: DOTO
 * @date 2018/3/26  16:06
 */
@Slf4j
@Data
@Service
@Transactional
public class WeatherService {

    @Resource
    private WeatherRepository weatherRepository;

    /**
     *
     * 新增天气
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public Weather save(Map<String, Object> payload) {
        Weather weather = null;
        try {
            Weather weather1 = (Weather) MapUtils.convertMap(Weather.class,payload);
            weather = weatherRepository.save(weather1);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return weather;
    }

    /**
     * 获取当前天气情况
     * */
    public List<Weather> getweather(String  cityCode){
        Sort sort = new Sort(Sort.Direction.DESC, "saveDate");
        List<Weather> weathers = weatherRepository.findByCityCode(cityCode , sort);
        return weathers;
    }
}
